package com.power.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.power.domain.SysLog;
import com.power.model.Result;
import com.power.service.SysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description 系统日志控制器
 * @Author dingchunlin
 * @Date 2024/07/13 13:34
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/sys/log")
@Api(tags = "系统操作日志接口管理")
public class SysLogController {

    @Autowired
    private SysLogService sysLogService;


    /**
     * 分页查询系统操作日志
     * @param current 页数
     * @param size 每页条数
     * @param userId 用户ID
     * @param operation 操作类型
     * @return 分页查询结果
     */
    @ApiOperation(value = "多条件分页查询系统操作日志")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('sys:log:page')")
    public Result<Page<SysLog>> loadSysLogPage(@RequestParam Long current,
                                               @RequestParam Long size,
                                               @RequestParam(required = false) Long userId,
                                               @RequestParam(required = false) String operation) {
        // 构造分页对象
        Page<SysLog> page = new Page<>(current, size);
        // 构造查询条件
        LambdaQueryWrapper<SysLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(userId), SysLog::getUserId, userId);
        lambdaQueryWrapper.like(StringUtils.hasText(operation), SysLog::getOperation, operation);
        lambdaQueryWrapper.orderByDesc(SysLog::getCreateDate);
        // 查询分页数据
        page = sysLogService.page(page, lambdaQueryWrapper);
        // 封装返回结果
        return Result.success(page);
    }

}
